<?php

namespace App\Http\Resources;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class WarehouseResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $users = $this->users;
        $wUsers = [];
        foreach ($users as $user) {
            $wUsers[] = [
                'id' => $user->user->id ?? 0,
                'realname' => $user->user->realname ?? ''
            ];
        }

        $depts = $this->depts;
        $wDepts = [];
        foreach ($depts as $dept) {
            $wDepts[] = [
                'id' => $dept->dept->id ?? 0,
                'name' => $dept->dept->name ?? ''
            ];
        }

        return [
            'id' => $this->id,
            'name' => $this->name,
            'w_number' => $this->w_number,
            'province_id' => $this->province_id,
            'province_name' => $this->province_name,
            'city_id' => $this->city_id,
            'city_name' => $this->city_name,
            'county_id' => $this->county_id,
            'county_name' => $this->county_name,
            'address' => $this->address,
            'create_time' => Carbon::parse($this->create_time)->toDateTimeString(),
            'update_time' => Carbon::parse($this->update_time)->toDateTimeString(),
            'users' => $wUsers,
            'depts' => $wDepts
        ];
    }
}
